﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using Swoosh.Contracts.MediaPlayerLib;
using Swoosh.Utilities.TraceLogger;

namespace Swoosh.Contracts.MediaPlayerLib
{
    public class SwooshAction
    {
        private List<Keys> _Trigger;
        public List<Keys> Trigger
        {
            get { return _Trigger; }
            set { _Trigger = value; }
        }

        private string _SourcePlayerName;
        public string SourcePlayerName
        {
            get { return _SourcePlayerName; }
            set { _SourcePlayerName = value; }
        }

        private string _DestinationPlayerName;
        public string DestinationPlayerName
        {
            get { return _DestinationPlayerName; }
            set { _DestinationPlayerName = value; }
        }        

        [XmlIgnore]
        public IPlaybackDevice Source
        {
            get { return SwooshConfig.CurrentConfig.IPlaybackDevices.Where(pd => pd.Name == SourcePlayerName).FirstOrDefault(); }
        }

        [XmlIgnore]
        public IPlaybackDevice Destination
        {
            get { return SwooshConfig.CurrentConfig.IPlaybackDevices.Where(pd => pd.Name == DestinationPlayerName).FirstOrDefault(); }
        }

        private object lockObject = new object();

        public SwooshAction()
        {
            _Trigger = new List<Keys>();
            _SourcePlayerName = "";
            _DestinationPlayerName = "";
        }

        public bool Swoosh()
        {
            lock (lockObject)
            {
                try
                {
                    Source.ReadPlayList();
                    Destination.Stop();
                    Destination.ClearPlaylist();
                    bool IsPlaying = false;
                    foreach (PlaylistItem item in Source.Playlist.Items)
                    {
                        PlaylistItem newitem = new PlaylistItem() { Position = item.Position, IsCurrent = item.IsCurrent, File = item.File, SeekTime = item.SeekTime, Label = item.Label };
                        newitem.File = Source.Storage.Convert(item.File, Destination.Storage.Name);
                        Destination.QueuePlaylistItem(newitem);
                        if (item.IsCurrent)
                        {
                            Destination.PlayPlaylistItem(newitem);
                            Source.StopPlayback();
                            IsPlaying = true;
                        }
                    }
                    if (!IsPlaying)
                    {
                        Destination.PlayPlaylistItem(new PlaylistItem() { Position = 0 });
                    }
                    return true;
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevels.Error, "Cannot execute Swoosh Action", e);
                    string ex = e.Message;
                    return false;
                }
            }
        }        
    }
}
